package com.qf.service.impl;

import com.alibaba.excel.EasyExcel;
import com.qf.model.entity.Manager;
import com.qf.dao.ManagerDao;
import com.qf.listener.ManagerListener;
import com.qf.service.ManagerService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (Manager)表服务实现类
 *
 * @author makejava
 * @since 2024-04-30 09:50:20
 */
@Service("managerService")
public class ManagerServiceImpl implements ManagerService {
    @Autowired
    private ManagerDao managerDao;

    /**
     * 通过ID查询单条数据
     *
     * @param mgrId 主键
     * @return 实例对象
     */
    @Override
    public Manager queryById(Long mgrId) {
        return this.managerDao.queryById(mgrId);
    }


    @Override
    public List<Manager> selectManagerByCondition(Map<String, Object> map) {
        return managerDao.selectManagerByCondition(map);
    }

    @Override
    public List<Manager> selectAllManager() {
        return managerDao.selectAllManager();
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<Manager> queryByPage(Long pageNum) {
        return this.managerDao.queryAllByLimit();
    }

    /**
     * 新增数据
     *
     * @param manager 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(Manager manager, HttpSession session) {
        Manager mgr = (Manager) session.getAttribute("manager");
        mgr.setCreateTime(new Date());
        int i = managerDao.insert(manager);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 批量导入
     * @param list
     * @return
     */
    @Override
    public int insertBatch(List<Manager> list) {
        return managerDao.insertBatch(list);
    }

    /**
     * 修改数据
     *
     * @param manager 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(Manager manager) {
        int i = managerDao.update(manager);
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 通过行条件删除数据
     *
     * @param account 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteByAccount(String account) {
        int i = managerDao.deleteByAccount(account);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteBatch(List<Integer> list) {
        int i = managerDao.deleteBatch(list);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Manager login(Manager manager, HttpServletRequest request) {

        // 要更新用户登录ip数据,需要调用request对象
        Manager mgr = managerDao.login(manager);

        if (mgr != null) {
            // 更新ip属性
            mgr.setLoginIp(request.getRemoteHost());
            // 更新登录时间
           // mgr.setLoginDate(new Date());
            // 更新完ip和登录时间后要整体更新数据库中的数据
            managerDao.update(mgr);
            return mgr;
        } else {
            return null;
        }

    }

    /**
     * 数据导出
     * @param response
     */
    @Override
    public void managerExport(HttpServletResponse response) {
        List<Manager> list = managerDao.selectAllManager();
        try {
            EasyExcel.write(response.getOutputStream(), Manager.class)
                    .sheet("管理员信息").doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean importManager(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), Manager.class,new ManagerListener(this))
                    .sheet("管理员信息").doReadSync();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
